/**
 * 
 */
package de.cbf.cam.dsa.battle.ui.composites;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

import de.cbf.cam.dsa.model.entities.WeaponStyle;
import de.cbf.cam.dsa.model.entities.BattleChar;
import de.cbf.cam.dsa.model.entities.weapon.WeaponMeleeInUse;

/**
 * @author Nebelritter
 *
 */
public class CompositeChooseWeaponTalent extends Composite {

	private CompositeWeaponDisplay mainWeaponDisplay;
	private CompositeWeaponTalentDisplay mainWeaponTalentDisplay;
	private CompositeWeaponDisplay secondWeaponDisplay;
	private CompositeWeaponTalentDisplay secondWeaponTalentDisplay;
	
	private List<ActionListener> selectionListeners = new ArrayList<ActionListener>(); 
	
	private BattleChar character;
	/**
	 * @param parent
	 * @param style
	 */
	public CompositeChooseWeaponTalent(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1,false);
		layout.marginWidth	= 0;
		layout.marginHeight	= 0;
		setLayout(layout);
		
	}
	private void buildHere(BattleChar character){
		GridLayout layout = new GridLayout(2,false);
		layout.marginWidth	= 0;
		layout.marginHeight	= 0;
		
		if(character.getMainWeapon()instanceof WeaponMeleeInUse){
			WeaponMeleeInUse weapon = (WeaponMeleeInUse)character.getMainWeapon();
			
			Group groupWeapon = new Group(this,SWT.NORMAL);
			groupWeapon.setLayoutData(new GridData(GridData.FILL_BOTH));
			groupWeapon.setLayout(layout);
			groupWeapon.setText("Hauptwaffe");
			
				mainWeaponDisplay = new CompositeWeaponDisplay(groupWeapon, SWT.NORMAL);
				GridData gridData = new GridData(GridData.FILL_BOTH);		
				mainWeaponDisplay.setLayoutData(gridData);
				
				mainWeaponTalentDisplay = new CompositeWeaponTalentDisplay(groupWeapon, SWT.NORMAL);
				gridData = new GridData(GridData.FILL_BOTH);		
				mainWeaponTalentDisplay.setLayoutData(gridData);
				
				mainWeaponTalentDisplay.setData(character,weapon);
				mainWeaponDisplay.setWeapon(character.getMainWeapon());
				
				mainWeaponTalentDisplay.registerSelectionListener(new MainWeaponTalentSelection());
		}
		if(character.getSecondWeapon()!=null){
			
			if(character.getSecondWeapon() instanceof WeaponMeleeInUse){
				WeaponMeleeInUse weapon = (WeaponMeleeInUse)character.getSecondWeapon();
				
				Group groupWeapon = new Group(this,SWT.NORMAL);
				groupWeapon.setLayoutData(new GridData(GridData.FILL_BOTH));
				groupWeapon.setLayout(layout);
				groupWeapon.setText("Nebenwaffe");
				
					secondWeaponDisplay = new CompositeWeaponDisplay(groupWeapon, SWT.NORMAL);
					GridData gridData = new GridData(GridData.FILL_BOTH);		
					mainWeaponDisplay.setLayoutData(gridData);
					
					secondWeaponTalentDisplay = new CompositeWeaponTalentDisplay(groupWeapon, SWT.NORMAL);
					gridData = new GridData(GridData.FILL_BOTH);		
					mainWeaponTalentDisplay.setLayoutData(gridData);
					
					secondWeaponTalentDisplay.setData(character,weapon);
					secondWeaponDisplay.setWeapon(character.getSecondWeapon());
					
					secondWeaponTalentDisplay.registerSelectionListener(new SecondWeaponTalentSelection());
			}
		}	
	}	
	
	
//========================
// 	SHORT property access
//========================

	public void setCharacter(BattleChar character) {
		this.character=character;
		buildHere(character);	
		if(mainWeaponTalentDisplay!=null)mainWeaponTalentDisplay.setFocus();
	}

	
	public boolean registerSelectionListener(ActionListener listener) {
		return selectionListeners.add(listener);
	}
	public boolean deRegisterSelectionListener(ActionListener listener) {
		return selectionListeners.remove(listener);
	}	

	class MainWeaponTalentSelection implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			WeaponStyle style = mainWeaponTalentDisplay.getSelectedStyle();
			if (character.getMainWeapon() instanceof WeaponMeleeInUse) {
				WeaponMeleeInUse weapon = (WeaponMeleeInUse) character.getMainWeapon();
				weapon.setActiveStyle(style);
				buildHere(character);
			}		
		}		
	}
	class SecondWeaponTalentSelection implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			WeaponStyle style = secondWeaponTalentDisplay.getSelectedStyle();			
			if (character.getSecondWeapon()	instanceof WeaponMeleeInUse) {
				WeaponMeleeInUse weapon = (WeaponMeleeInUse) character.getSecondWeapon();
				weapon.setActiveStyle(style);
				buildHere(character);
			}		
		}		
	}
}
